package com.senthink.gl.business.domain.service;

import com.senthink.gl.business.forgetpw.ForgetPassword;
import com.senthink.gl.business.common.exception.DataAccessException;
import com.senthink.gl.business.domain.repository.ForgetPasswordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 重置密码DataService
 * @author yuanpeng@senthink.com
 * @data 2018-01-24 15:33
 */

@Service
public class ForgetPasswordDataService {

    @Autowired
    private ForgetPasswordRepository repository;

    public ForgetPassword save(ForgetPassword body) {
        try {
            return repository.save(body);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public Optional<ForgetPassword> findTopByAccount(String account) {
        try {
            return repository.findTopByAccount(account);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public long isResetPw(String account, String code, boolean isReset) {
        try {
            return repository.countByAccountAndCodeAndReset(account, code, isReset);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public void delete(String account) {
        try {
            repository.deleteByAccount(account);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }
}
